package com.xszx.system.controller;

import com.xszx.server.dto.AjaxJson;
import com.xszx.server.dto.Page;
import com.xszx.server.dto.RoleDto;
import com.xszx.server.entity.Role;
import com.xszx.server.service.RoleResourceService;
import com.xszx.server.service.RoleService;
import com.xszx.server.service.RoleUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @Resource
    private RoleResourceService roleResourceService;

    @Resource
    private RoleUserService roleUserService;

    @PostMapping("/getAllRoles")
    public Page getAllRoles(@RequestBody Page page){
        return roleService.getAllRoles(page);
    }

    @GetMapping("/getRole")
    public AjaxJson getRole(String id){
        return AjaxJson.success().put("roles",roleService.getRole(id));
    }

    @PostMapping("/save")
    public AjaxJson save(@RequestBody Role role){
        return AjaxJson.success(roleService.saveOrUpdate(role));
    }
    @GetMapping("/deleteRole")
    public AjaxJson deleteRole(String id){
        int result = roleService.del(id);
        if(result>0){
            return AjaxJson.success("删除成功");
        }else{
            return AjaxJson.error("删除失败");
        }
    }
    //保存角色资源信息
    @PostMapping("/save-resources")
    public AjaxJson saveResources(@RequestBody RoleDto roleDto){
        roleResourceService.saveResources(roleDto);
        return AjaxJson.success("保存成功");
    }

    /**
     * 根据角色id查询资源
     * @param roleId
     * @return
     */
    @GetMapping("/list-resource/{roleId}")
    public AjaxJson listResources(@PathVariable("roleId") String roleId){
        return AjaxJson.success().put("resourceIds",roleResourceService.getResourcesByRoleId(roleId));
    }

    //保存角色用户信息
    @PostMapping("/save-user")
    public AjaxJson saveUsers(@RequestBody RoleDto roleDto){
        roleUserService.saveUsers(roleDto);
        return AjaxJson.success("保存成功");
    }

    /**
     * 根据角色id查询资源
     * @param roleId
     * @return
     */
    @GetMapping("/list-users/{roleId}")
    public AjaxJson listUsers(@PathVariable("roleId") String roleId){
        return AjaxJson.success().put("userIds",roleUserService.getUsersByRoleId(roleId));
    }



}
